package org.jeecg.modules.yw.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.modules.yw.entity.Article;
import org.jeecg.modules.yw.result.Result;
import org.jeecg.modules.yw.service.ArticleService;
import org.jeecg.modules.yw.utils.CommonUtils;
import org.jeecg.modules.yw.utils.DateUtils;
import org.jeecg.modules.yw.utils.UserUtil;
import org.jeecg.modules.yw.vo.ArticleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Api(tags = "文章管理")
@RequestMapping("/app/article")
public class ArticleController {
    @Resource
    private ArticleService articleService;
    @Autowired
    private ISysBaseAPI sysBaseAPI;
    @ApiOperation(value = "获取文章列表")
    @GetMapping("/page")
    public  org.jeecg.common.api.vo.Result page(Page<Article> page,
                                                @RequestParam(value = "category", required = false) String category,
                                                @RequestParam(value = "name", required = false) String name,
                                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize ) {
        page.setCurrent(pageNo);
        page.setSize(pageSize);
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        if(name != null) {
            queryWrapper.like("name", name);
        }
        if(category != null) {
            queryWrapper.eq("category", category);
        }
        queryWrapper.orderByDesc("createtime");
        IPage<Article> result = articleService.page(page, queryWrapper);
        return  org.jeecg.common.api.vo.Result.ok(result);
    }
    @ApiOperation(value = "获取文章列表")
    @GetMapping("/List")
    public Result list(Page<Article> page) {
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("name");
        List<Article> result = articleService.list(queryWrapper);
        return Result.success(result);
    }
    @ApiOperation(value = "添加文章")
    @PostMapping("/addArticle")
    public Result addArticle(@RequestBody ArticleVo articleVo,HttpServletRequest request) throws Exception {
        Article article = new Article();
        BeanUtils.copyProperties(articleVo, article);
        article.setId(CommonUtils.getID());
        article.setCreatetime(DateUtils.getCurrentDate());
        article.setCreator(UserUtil.getUserId(request, sysBaseAPI));
        articleService.save(article);
        return Result.success();
    }
    @ApiOperation(value = "修改文章")
    @PostMapping("/editArticle")
    public Result editArticle(@RequestBody ArticleVo articleVo) throws Exception {
        Article article = new Article();
        BeanUtils.copyProperties(articleVo, article);
        articleService.updateById(article);
        return Result.success();
    }
    @ApiOperation(value = "删除文章")
    @DeleteMapping("/deleteArticle")
    public org.jeecg.common.api.vo.Result deleteArticle(@RequestParam(value = "id") String id) {
        articleService.removeById(id);
        return org.jeecg.common.api.vo.Result.ok("删除成功");
    }
}
